﻿using _IMES.DBUtils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using houseAgency.Base;
using houseAgency.Models;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Type = houseAgency.Models.Type;

namespace houseAgency.ViewModel.SeekTenants
{
    public class SelectViewModel : ObservableObject
    {
        public SelectViewModel()
        {
            CurrentHouse = new();
            IsChecked = new();
            for (int i = 0; i < 11; i++)
            {
                IsChecked.Add(false);
            }
            GetAllType();
            GetAllSeat();
            GetAllFloor();
            GetAllMothed();
            GetAllFavor();
            GetAllFitment();
            SelectByInfo();
        }
        #region 属性
        private House currentHouse;

        public House CurrentHouse
        {
            get { return currentHouse; }
            set
            {
                currentHouse = value;
                OnPropertyChanged();
            }
        }
        private House selectedHouse;

        public House SelectedHouse
        {
            get { return selectedHouse; }
            set
            {
                selectedHouse = value;
                if (value != null)
                {
                    CurrentHouse = selectedHouse;
                }
                OnPropertyChanged();
            }
        }

        private ObservableCollection<bool> isChecked;

        public ObservableCollection<bool> IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                OnPropertyChanged();
            }
        }
        private List<HouseInfoCheck> houseInfo;

        public List<HouseInfoCheck> HouseInfo
        {
            get { return houseInfo; }
            set
            {
                houseInfo = value;
                OnPropertyChanged();
            }
        }
        private List<House> houseList;

        public List<House> HouseList
        {
            get { return houseList; }
            set
            {
                houseList = value;
                OnPropertyChanged();
            }
        }
        private List<Type> typeList = new();

        public List<Type> TypeList
        {
            get { return typeList; }
            set
            {
                typeList = value;
                OnPropertyChanged();
            }
        }

        private List<Seat> seatList = new List<Seat>();

        public List<Seat> SeatList
        {
            get { return seatList; }
            set
            {
                seatList = value;
                OnPropertyChanged();
            }
        }

        private List<Floor> floorList = new List<Floor>();

        public List<Floor> FloorList
        {
            get { return floorList; }
            set
            {
                floorList = value;
                OnPropertyChanged();
            }
        }

        private List<Mothed> mothedList = new List<Mothed>();

        public List<Mothed> MothedList
        {
            get { return mothedList; }
            set
            {
                mothedList = value;
                OnPropertyChanged();
            }
        }

        private List<Favor> favorList = new List<Favor>();

        public List<Favor> FavorList
        {
            get { return favorList; }
            set
            {
                favorList = value;
                OnPropertyChanged();
            }
        }

        private List<Fitment> fitmentList = new List<Fitment>();

        public List<Fitment> FitmentList
        {
            get { return fitmentList; }
            set
            {
                fitmentList = value;
                OnPropertyChanged();
            }
        }

        #endregion

        #region 命令 Command
        public ICommand OKCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    HouseInfo = new List<HouseInfoCheck>() 
                    {
                        new HouseInfoCheck(){ Name = "companyName", Value = CurrentHouse.CompanyName, IsChecked = IsChecked[0]},
                        new HouseInfoCheck(){ Name = "buildYear", Value = CurrentHouse.BuildYear, IsChecked = IsChecked[1]},
                        new HouseInfoCheck(){ Name = "area", Value = CurrentHouse.Area, IsChecked = IsChecked[2]},
                        new HouseInfoCheck(){ Name = "floorID", Value = CurrentHouse.FloorID, IsChecked = IsChecked[3]},
                        new HouseInfoCheck(){ Name = "typeID", Value = CurrentHouse.TypeID, IsChecked = IsChecked[4]},
                        new HouseInfoCheck(){ Name = "seatID", Value = CurrentHouse.SeatID, IsChecked = IsChecked[5]},
                        new HouseInfoCheck(){ Name = "favorID", Value = CurrentHouse.FavorID, IsChecked = IsChecked[6]},
                        new HouseInfoCheck(){ Name = "fitmentID", Value = CurrentHouse.FitmentID, IsChecked = IsChecked[7]},
                        new HouseInfoCheck(){ Name = "mothedID", Value = CurrentHouse.MothedID, IsChecked = IsChecked[8]},
                        new HouseInfoCheck(){ Name = "price", Value = CurrentHouse.Price.ToString(), IsChecked = IsChecked[9]},
                        new HouseInfoCheck(){ Name = "id", Value = CurrentHouse.ID, IsChecked = IsChecked[10]}
                    };
                    SelectByInfo();
                });
            }
        }

        public ICommand ClearAllCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    CurrentHouse = new();
                    for (int i = 0; i < IsChecked.Count; i++)
                    {
                        IsChecked[i] = false;
                    }
                });
            }
        }
        public ICommand SelectAllCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    for (int i = 0; i < IsChecked.Count; i++)
                    {
                        IsChecked[i] = true;
                    }
                });
            }
        }
        public ICommand MouseDoubleCommand
        {
            get
            {
                return new RelayCommand<House>(o =>
                {
                    if (o != null)
                    {
                        Global.selectedHouse = o;
                        WindowManager.Show("House", null);
                    }
                });
            }
        }
        #endregion

        #region 方法
        private void SelectByInfo()
        {
            string sql = "select * from house where 1 = 1 ";
            List<MySqlParameter> paraList = new List<MySqlParameter>();

            if (HouseInfo != null)
            {
                for (int i = 0; i < HouseInfo.Count; i++)
                {
                    if (HouseInfo[i].IsChecked == true)
                    {
                        sql += " and " + HouseInfo[i].Name + " like @" + HouseInfo[i].Name;
                        paraList.Add(new MySqlParameter(HouseInfo[i].Name, "%" + (HouseInfo[i].Value ?? "") + "%"));
                    }

                }
            }
            DataTable dataTable;
            if (paraList == null || paraList.Count == 0)
            {
                dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            }
            else
            {
                dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql, paraList.ToArray());
            }
            
            if (dataTable != null)
            {
                HouseList = DBConvert.DataTableToList<House>(dataTable);
                if (HouseList != null && HouseList.Count != 0)
                {
                    foreach (House house in HouseList)
                    {
                        GetNameByID(house);
                    }
                }
            }

        }

        private void GetAllType()
        {
            string sql = "select * from type";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.TypeList = DBConvert.DataTableToList<Type>(dataTable);
            }
        }
        private void GetAllSeat()
        {
            string sql = "select * from seat";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.SeatList = DBConvert.DataTableToList<Seat>(dataTable);
            }
        }
        private void GetAllFloor()
        {
            string sql = "select * from floor";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.FloorList = DBConvert.DataTableToList<Floor>(dataTable);
            }
        }
        private void GetAllMothed()
        {
            string sql = "select * from mothed";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.MothedList = DBConvert.DataTableToList<Mothed>(dataTable);
            }
        }
        private void GetAllFavor()
        {
            string sql = "select * from favor";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.FavorList = DBConvert.DataTableToList<Favor>(dataTable);
            }
        }
        private void GetAllFitment()
        {
            string sql = "select * from fitment";
            DataTable dataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            if (dataTable != null)
            {
                this.FitmentList = DBConvert.DataTableToList<Fitment>(dataTable);
            }
        }
        private void GetNameByID(House house)
        {
            string sql;
            if (house.TypeID != null && house.TypeID != "")
            {
                sql = "select * from type where id = @id";
                DataRow dataRow = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id",house.TypeID));
                if (dataRow != null)
                {
                    house.TypeName = DBConvert.DataRowToModel<Type>(dataRow).Name ?? "";
                }
            }
            if (house.FloorID != null && house.FloorID != "")
            {
                sql = "select * from floor where id = @id";
                DataRow dataRow = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", house.FloorID));
                if (dataRow != null)
                {
                    house.FloorName = DBConvert.DataRowToModel<Floor>(dataRow).Name ?? "";
                }
            }
            if (house.SeatID != null && house.SeatID != "")
            {
                sql = "select * from seat where id = @id";
                DataRow dataRow = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", house.SeatID));
                if (dataRow != null)
                {
                    house.SeatName = DBConvert.DataRowToModel<Seat>(dataRow).Name ?? "";
                }
            }
            if (house.FavorID != null && house.FavorID != "")
            {
                sql = "select * from favor where id = @id";
                DataRow dataRow = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", house.FavorID));
                if (dataRow != null)
                {
                    house.FavorName = DBConvert.DataRowToModel<Favor>(dataRow).Name ?? "";
                }
            }
            if (house.FitmentID != null && house.FitmentID != "")
            {
                sql = "select * from fitment where id = @id";
                DataRow dataRow = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", house.FitmentID));
                if (dataRow != null)
                {
                    house.FitmentName = DBConvert.DataRowToModel<Fitment>(dataRow).Name ?? "";
                }
            }
            if (house.MothedID != null && house.MothedID != "")
            {
                sql = "select * from mothed where id = @id";
                DataRow dataRow = MySQLHelper.ExecuteDataRow(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", house.MothedID));
                if (dataRow != null)
                {
                    house.MothedName = DBConvert.DataRowToModel<Mothed>(dataRow).Name ?? "";
                }
            }
        }
        #endregion
    }

    public class HouseInfoCheck
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public bool IsChecked { get; set; }
    }
}
